﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class RoyalItemRequestDAL : IRoyalItemRequestDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public RoyalItemRequestDAL()
        {
        }

        public IList<RoyalItemRequestInfo> ListRoyalItemRequest(RoyalItemRequestInfo reqInfo)
        {
            string strSQL;
            int reqYear = 0;
            int empType = 0;

            if (reqInfo.RequestYear > 0)
                reqYear = reqInfo.RequestYear;
            if (reqInfo.EmployeeType != null)
                empType = reqInfo.EmployeeType.Sequence;

            List<RoyalItemRequestInfo> infoList = new List<RoyalItemRequestInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", reqInfo.Sequence);
                dbManager.AddParameters(2, "@ReguestYear", reqYear);
                dbManager.AddParameters(3, "@EmpTypeSequence", empType);

                strSQL = "SELECT rq.REQ_SEQ, rq.EMP_SEQ, rq.POS_SEQ, rq.CLASS_SEQ, rq.JOB_SITE_SEQ"
                        + ", rq.ROYAL_ITEM_SEQ, rq.CURR_ROYAL_ITEM_SEQ, rq.REQ_YEAR, rq.SALARY, rq.NEED"
                        + ", rq.REMARKS, rq.RECORD_STATUS, rq.CREATED_BY, rq.UPDATED_BY"
                    + " FROM HRM_PS_ROYAL_ITEM_REQUESTS rq "
                    + " LEFT JOIN HRM_PS_EMPLOYEES e ON rq.EMP_SEQ = e.EMP_SEQ"
                    + " WHERE rq.RECORD_STATUS not like @DeleteRecord"
                    + (reqInfo.Sequence > 0 ? " AND rq.REQ_SEQ = @Sequence" : "")
                    + (reqYear > 0 ? " AND rq.REQ_YEAR = @ReguestYear" : "")
                    + (empType > 0 ? " AND e.EMP_TYPE_SEQ = @EmpTypeSequence" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    EmployeeDAL empDAL = new EmployeeDAL();
                    EmployeeInfo empInfo = new EmployeeInfo();
                    if (!rdr.IsDBNull(1))
                    {
                        IList<EmployeeInfo> empList = empDAL.ListEmployees(new EmployeeInfo(rdr.GetInt32(1)));
                        empInfo = empList[0];
                    }
                    PositionDAL posDAL = new PositionDAL();
                    PositionInfo posInfo = new PositionInfo();
                    if (!rdr.IsDBNull(2))
                    {
                        IList<PositionInfo> posList = posDAL.ListPositions(new PositionInfo(rdr.GetInt32(2)));
                        posInfo = posList[0];
                    }
                    ClassDAL classDAL = new ClassDAL();
                    ClassInfo classInfo = new ClassInfo();
                    if (!rdr.IsDBNull(3))
                    {
                        IList<ClassInfo> classList = classDAL.ListClasses(new ClassInfo(rdr.GetInt32(3)));
                        classInfo = classList[0];
                    }
                    JobSiteDAL jsDAL = new JobSiteDAL();
                    JobSiteInfo jsInfo = new JobSiteInfo();
                    if (!rdr.IsDBNull(4))
                    {
                        IList<JobSiteInfo> jsList = jsDAL.ListJobSite(new JobSiteInfo(rdr.GetInt32(4)));
                        jsInfo = jsList[0];
                    }

                    RoyalItemDAL riDAL = new RoyalItemDAL();
                    RoyalItemInfo riInfo = new RoyalItemInfo();
                    if (!rdr.IsDBNull(5))
                    {
                        IList<RoyalItemInfo> riList = riDAL.ListRoyalItems(new RoyalItemInfo(rdr.GetInt32(5)));
                        riInfo = riList[0];
                    }
                    RoyalItemInfo criInfo = new RoyalItemInfo();
                    if (!rdr.IsDBNull(6))
                    {
                        IList<RoyalItemInfo> riList = riDAL.ListRoyalItems(new RoyalItemInfo(rdr.GetInt32(6)));
                        criInfo = riList[0];
                    }

                    RoyalItemRequestInfo Info = new RoyalItemRequestInfo(rdr.GetInt32(0)
                            , empInfo, posInfo, classInfo, jsInfo, riInfo, criInfo
                            , rdr.IsDBNull(7) ? 0 : (int)rdr.GetDecimal(7)
                            , rdr.IsDBNull(8) ? (double?)null : rdr.GetDouble(8)
                            , rdr.GetInt32(9) == 1
                            , rdr.IsDBNull(10) ? "" : rdr.GetString(10)
                            , rdr.IsDBNull(11) ? "" : rdr.GetString(11)
                            , rdr.IsDBNull(12) ? null : (int?)rdr.GetInt32(12)
                            , rdr.IsDBNull(13) ? null : (int?)rdr.GetInt32(13));
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }
        
        public void Insert(RoyalItemRequestInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(13);

                dbManager.AddParameters(0, "@ReqYear", Info.RequestYear);
                dbManager.AddParameters(1, "@EmpSeq", Info.Employee == null ? (int?)null : Info.Employee.Sequence);
                dbManager.AddParameters(2, "@PosSeq", Info.Position == null ? (int?)null : Info.Position.Sequence);
                dbManager.AddParameters(3, "@ClassSeq", Info.Class == null ? (int?)null : Info.Class.Sequence);
                dbManager.AddParameters(4, "@JobSiteSeq", Info.JobSite == null ? (int?)null : Info.JobSite.Sequence);
                dbManager.AddParameters(5, "@Salary", Info.Salary);
                dbManager.AddParameters(6, "@ReqRoyalItemSeq", Info.RoyalItem == null ? (int?)null : Info.RoyalItem.Sequence);
                dbManager.AddParameters(7, "@CurrRoyalItemSeq", Info.CurrentRoyalItem == null ? (int?)null : Info.CurrentRoyalItem.Sequence);
                dbManager.AddParameters(8, "@Need", Info.Need);
                dbManager.AddParameters(9, "@Remarks", Info.Remarks);
                dbManager.AddParameters(10, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(11, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(12, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_PS_ROYAL_ITEM_REQUESTS(REQ_YEAR, EMP_SEQ"
                        + ", POS_SEQ, CLASS_SEQ, JOB_SITE_SEQ, SALARY, ROYAL_ITEM_SEQ, CURR_ROYAL_ITEM_SEQ"
                        + ", NEED, REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@ReqYear, @EmpSeq, @PosSeq, @ClassSeq, @JobSiteSeq, @Salary, @ReqRoyalItemSeq"
                        + ", @CurrRoyalItemSeq, @Need, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(RoyalItemRequestInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(13);

                dbManager.AddParameters(0, "@Sequence", Info.Sequence);
                dbManager.AddParameters(1, "@ReqYear", Info.RequestYear);
                dbManager.AddParameters(2, "@EmpSeq", Info.Employee == null ? (int?)null : Info.Employee.Sequence);
                dbManager.AddParameters(3, "@PosSeq", Info.Position == null ? (int?)null : Info.Position.Sequence);
                dbManager.AddParameters(4, "@ClassSeq", Info.Class == null ? (int?)null : Info.Class.Sequence);
                dbManager.AddParameters(5, "@JobSiteSeq", Info.JobSite == null ? (int?)null : Info.JobSite.Sequence);
                dbManager.AddParameters(6, "@Salary", Info.Salary);
                dbManager.AddParameters(7, "@ReqRoyalItemSeq", Info.RoyalItem == null ? (int?)null : Info.RoyalItem.Sequence);
                dbManager.AddParameters(8, "@CurrRoyalItemSeq", Info.CurrentRoyalItem == null ? (int?)null : Info.CurrentRoyalItem.Sequence);
                dbManager.AddParameters(9, "@Need", Info.Need);
                dbManager.AddParameters(10, "@Remarks", Info.Remarks);
                dbManager.AddParameters(11, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(12, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_ROYAL_ITEM_REQUESTS"
                    + " SET REQ_YEAR = @ReqYear"
                        + ", EMP_SEQ = @EmpSeq"
                        + ", POS_SEQ = @PosSeq"
                        + ", CLASS_SEQ = @ClassSeq"
                        + ", JOB_SITE_SEQ = @JobSiteSeq"
                        + ", SALARY = @Salary"
                        + ", ROYAL_ITEM_SEQ = @ReqRoyalItemSeq"
                        + ", CURR_ROYAL_ITEM_SEQ = @CurrRoyalItemSeq"
                        + ", NEED = @Need"
                        + ", REMARKS = @Remarks"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE REQ_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdateWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_ROYAL_ITEM_REQUESTS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdateWhen"
                    + " WHERE REQ_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
